/* eslint-disable @typescript-eslint/no-empty-interface */
import { IGetFlowResultExecDetailsRes } from '@/api';
import { defineStore } from 'pinia';

type TNodeResultItem = {
  nodeId: string;
  nodeType: string;
  nodeName: string;
  errorMsg: string[];
  execStatus: string;
} ;
type TEdgeResultItem = Required<Required<IGetFlowResultExecDetailsRes>['lineResults'][0]>;

export interface IResultOfNodeItem extends TNodeResultItem {}

interface IResultOfEdgeItem extends TEdgeResultItem {}

export type TverificationItem = IResultOfEdgeItem & IResultOfNodeItem;

export const useNodeVerification = defineStore('verification-results', {
  state() {
    return {
      /** 是否处于执行测试状态 */
      isExecuting: false,
      resultsOfList: [] as TverificationItem[]
    };
  },
  actions: {
    addResults(results: TverificationItem) {
      const sameIndex = this.resultsOfList.findIndex((item) => item.nodeId === results.nodeId && item.execStatus === 'WAITING');
      if (sameIndex > -1) {
        this.resultsOfList.splice(sameIndex, 1, results);
      } else {
        this.resultsOfList.push(results);
      }
    },
    clearResults() {
      this.resultsOfList = [];
      this.isExecuting = false;
    }
  },
  getters: {
    hasExecResult({ resultsOfList }) {
      return resultsOfList.length > 0;
    }
  }
});
